package com.kevin.reflection;

import java.lang.reflect.InvocationTargetException;

public class ReflectionTest {

    public static void main(String[] args) {
        User user = null;
        try {
            Class clazz = Class.forName("com.kevin.reflection.User");
            user = (User)clazz.getConstructor(String.class,String.class).newInstance("admin","123456");
            clazz.getClass().getMethod("setUsername", String.class).invoke(user,"root");
            clazz.getClass().getDeclaredField("password").set(user,"654321");
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        System.out.println(user);
    }


}
